package repository

import (
	"gorm.io/gorm"
	"shyxy-model-agent/common/cache/lru"
	"shyxy-model-agent/core/model"
)

type modelContainerInstanceRepository struct {
	*BaseRepository
}

func NewModelContainerInstanceRepository(db *gorm.DB) ModelContainerInstanceRepository {
	return &modelContainerInstanceRepository{
		BaseRepository: NewBaseRepository(db),
	}
}

func (mcir *modelContainerInstanceRepository) Create(instance *model.ModelContainerInstance) error {
	return mcir.GetDB().Create(instance).Error
}

func (mcir *modelContainerInstanceRepository) FindListByStatusAndModelName(modelName string, status ...string) ([]*model.ModelContainerInstance, error) {
	var instances []*model.ModelContainerInstance
	query := mcir.GetDB().Where("model_name = ?", modelName)
	if len(status) > 0 {
		query = query.Where("status IN (?)", status)
	}
	err := query.Find(&instances).Error
	return instances, err
}

type cachedModelContainerInstanceRepository struct {
	*BaseCachedRepository
}

func (cmir *cachedModelContainerInstanceRepository) CachedRepository() {}

func NewCachedModelContainerInstanceRepository(db *gorm.DB, cache *lru.LRU) ModelContainerInstanceRepository {
	return &cachedModelContainerInstanceRepository{
		BaseCachedRepository: NewBaseCachedRepository(db, cache, "model_container_instance"),
	}
}

func (cmir *cachedModelContainerInstanceRepository) Create(instance *model.ModelContainerInstance) error {
	return (&modelContainerInstanceRepository{BaseRepository: NewBaseRepository(cmir.GetDB())}).Create(instance)
}
func (cmir *cachedModelContainerInstanceRepository) FindListByStatusAndModelName(modelName string, status ...string) ([]*model.ModelContainerInstance, error) {
	return (&modelContainerInstanceRepository{BaseRepository: NewBaseRepository(cmir.GetDB())}).FindListByStatusAndModelName(modelName, status...)
}
func NewModelContainerInstanceRepositoryWithCache(db *gorm.DB, cache *lru.LRU) ModelContainerInstanceRepository {
	if cache != nil {
		return NewCachedModelContainerInstanceRepository(db, cache)
	}
	return NewModelContainerInstanceRepository(db)
}
